package io.task.message.socket.thread.pool;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Queue;
import java.util.concurrent.Executor;


public abstract class QueueModeSynchronousTask<T> extends SynchronousTask {
    private static final Logger log = LoggerFactory.getLogger(QueueModeSynchronousTask.class);

    public QueueModeSynchronousTask(Executor executor) {
        super(executor);
    }


    public boolean addMsg(T t) {
        if (this.isCanceled()) {
            log.error("任务已经取消");
            return false;
        }

        return getMsgQueue().add(t);
    }

    /**
     * 清空处理的队列消息
     */
    public void clearMsgQueue() {
        if (getMsgQueue() != null) {
            getMsgQueue().clear();
        }
    }

    @Override
    public boolean isNeededExecute() {
        return (getMsgQueue() != null && !getMsgQueue().isEmpty()) && !this.isCanceled();
    }

    /**
     * 获取消息队列
     */
    public abstract Queue<T> getMsgQueue();
}
